package cn.Y_Qiu.org.service.impl;

import cn.Y_Qiu.basic.service.impl.BaseServiceImpl;
import cn.Y_Qiu.org.domain.Department;
import cn.Y_Qiu.org.mapper.DepartmentMapper;
import cn.Y_Qiu.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper mapper;

    @Override
    // 默认为这个样子,也可不写括号
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public void add(Department department) {
        mapper.add(department);
        if (department.getParent() == null){
            department.setDirPath("/" + department.getId());
        } else {
            //在xml里做了
           // department.setParent_id(department.getParent().getId());
            String parentdirpath =  mapper.queryById(department.getParent().getId()).getDirPath();
            department.setDirPath(parentdirpath + "/" + department.getId());
        }
        mapper.update(department);
    }


    @Override
    @Transactional
    public void update(Department department) {

        if (department.getParent() == null){
            department.setDirPath("/" + department.getId());
        } else {
            String parentdirpath =  mapper.queryById(department.getParent().getId()).getDirPath();
            department.setDirPath(parentdirpath + "/" + department.getId());
        }
        mapper.update(department);


    }

    @Override
    public List<Department> queryDepartmentTree() {
        return mapper.queryDepartmentTree();
    }

}
